/*
 * FactoryTower.java
 *
 * Created on December 9, 2007, 3:48 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.tower;

import hextd.hex.HexTile;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import com.usoog.tdcore.exception.TileAlreadyHasTowerException;
import com.usoog.tdcore.exception.TileNotBuildableException;
import com.usoog.tdcore.player.Player;
import hextd.core.GameStateImplementation;

/**
 *
 * @author hylke
 */
public class FactoryTower {

	public static class TowerCompare implements Comparator<HexTDTower> {

		@Override
		public int compare(HexTDTower o1, HexTDTower o2) {
			int i1, i2;
			i1 = o1.getTile().getGridCol();
			i2 = o2.getTile().getGridCol();
			if (i1 != i2) {
				return i1 - i2;
			}
			i1 = o1.getTile().getGridRow();
			i2 = o2.getTile().getGridRow();
			if (i1 != i2) {
				return i1 - i2;
			}
			System.err.println("TowerFactory::TowerCompare: Towers are the same!");
			return 0;
		}
	}
	public static TowerCompare towerComparator = new TowerCompare();

	public static enum type {

		firecontrol("fc", TowerFireControl.price, TowerFireControl.name),
		upgradeDamage("ud", TowerUpgradeDamage.price, TowerUpgradeDamage.name),
		upgradeRange("ur", TowerUpgradeRange.price, TowerUpgradeRange.name),
		greenOne("g1", TowerGreenOne.price, TowerGreenOne.name),
		greenTwo("g2", TowerGreenTwo.price, TowerGreenTwo.name),
		greenThree("g3", TowerGreenThree.price, TowerGreenThree.name),
		redOne("r1", TowerRedOne.price, TowerRedOne.name),
		redTwo("r2", TowerRedTwo.price, TowerRedTwo.name),
		redThree("r3", TowerRedThree.price, TowerRedThree.name),
		yellowOne("y1", TowerYellowOne.price, TowerYellowOne.name),
		yellowTwo("y2", TowerYellowTwo.price, TowerYellowTwo.name),
		yellowThree("y3", TowerYellowThree.price, TowerYellowThree.name),
		blueOne("b1", TowerBlueOne.price, TowerBlueOne.name),
		blueTwo("b2", TowerBlueTwo.price, TowerBlueTwo.name),
		blueThree("b3", TowerBlueThree.price, TowerBlueThree.name),
		homeBase("hb", TowerHome.price, TowerHome.name);
		/**
		 * price: the base price of a new tower of this type.
		 */
		public final Map<String, Long> price;
		/**
		 * code: the two letter code of this tower type.
		 */
		public final String code;
		/**
		 * name: The name of the tower
		 */
		public final String name;

		private type(String code, Map<String, Long> price, String name) {
			this.price = price;
			this.code = code;
			this.name = name;
		}
	}
	public static final Map<String, type> table;

	static {
		table = new HashMap<String, type>();
		for (type t : type.values()) {
			table.put(t.code, t);
		}
	}

	public static HexTDTower createTower(
			type t,
			GameStateImplementation gs,
			HexTile h,
			Player owner,
			int activateTick)
			throws TileAlreadyHasTowerException, TileNotBuildableException {
		HexTDTower tower = null;
		switch (t) {
			case upgradeDamage:
				tower = new TowerUpgradeDamage();
			case upgradeRange:
				tower = new TowerUpgradeRange();
			case firecontrol:
				tower = new TowerFireControl();
			case greenOne:
				tower = new TowerGreenOne();
			case greenTwo:
				tower = new TowerGreenTwo();
			case greenThree:
				tower = new TowerGreenThree();
			case redOne:
				tower = new TowerRedOne();
			case redTwo:
				tower = new TowerRedTwo();
			case redThree:
				tower = new TowerRedThree();
			case yellowOne:
				tower = new TowerYellowOne();
			case yellowTwo:
				tower = new TowerYellowTwo();
			case yellowThree:
				tower = new TowerYellowThree();
			case blueOne:
				tower = new TowerBlueOne();
			case blueTwo:
				tower = new TowerBlueTwo();
			case blueThree:
				tower = new TowerBlueThree();
			case homeBase:
				tower = new TowerHome();
		}
		tower.init(gs, h, owner, activateTick);
		return tower;
	}
}
